package com.youmingxing.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.network.AccountApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.utils.ActivityJump;
import com.youmingxing.utils.Tools;
import com.youmingxing.view.TextCountWatcher;

import org.json.JSONObject;

/**
 * Created by JHS on 2015/9/9.
 * Edited by ChenZhixi
 */
public class RegisterByMobileActivity extends BaseActivity {

    TextView getCode;
    EditText codeEditText;
    Button registerBtn;
    EditText phoneNumber;

    String number;
    String code;

    int type; //0 注册 1 修改手机号 2绑定手机号
    public static final String EXTRAS_TYPE = "type"; //0 注册 1 修改手机号 2绑定手机号
    public static final int TYPE_REGISTER = 0;
    public static final int TYPE_MODIFY = 1;
    public static final int TYPE_BIND = 2;
    public static final int TYPE_FORGET_PWD = 3;
    boolean numberChecked = false;
    boolean codeChecked = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_register);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getInt("type");
        }
        initView();
    }

    private void initView() {
        TextView jumpLogin = (TextView) findViewById(R.id.jump_login_tv);
        registerBtn = (Button) findViewById(R.id.regist_btn);
        getCode = (TextView) findViewById(R.id.get_verfy);
        phoneNumber = (EditText) findViewById(R.id.regist_edt);
        codeEditText = (EditText) findViewById(R.id.verfy_edt);
        if (type == TYPE_REGISTER) {
            setTopbarName("手机号注册");
        } else if (type == TYPE_MODIFY) {
            setTopbarName("修改手机号");
            registerBtn.setText("下一步");
            jumpLogin.setVisibility(View.GONE);
        } else if (type == TYPE_BIND) {
            setTopbarName("绑定手机号");
            registerBtn.setText("下一步");
            jumpLogin.setVisibility(View.GONE);
        } else if (type == TYPE_FORGET_PWD) {
            setTopbarName("重置密码");
            registerBtn.setText("下一步");
            jumpLogin.setVisibility(View.GONE);
        }

        jumpLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityJump.getInsanceJump(RegisterByMobileActivity.this, LoginByMobleActivity.class, null, true);
            }
        });

        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = phoneNumber.getText().toString();
                android.util.Log.i("register", number);
                if (Tools.isMobileNum(number)) {
                    AccountApi.getRegisterCode(number, codeHandler);
                    new CountDownTimer(60000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {
                            getCode.setText(millisUntilFinished / 1000 + "秒");
                            getCode.setClickable(false);
                        }

                        @Override
                        public void onFinish() {
                            getCode.setText("重新获取");
                            getCode.setClickable(true);
                        }
                    }.start();
                } else {
                    showToast(R.string.not_moble_num);
                }
            }
        });

        phoneNumber.addTextChangedListener(new TextCountWatcher(11) {

            @Override
            public void onTextMatchCount() {
                numberChecked = true;
                if (codeChecked) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                numberChecked = false;
                notClick();
            }
        });
        codeEditText.addTextChangedListener(new TextCountWatcher(6) {
            @Override
            public void onTextMatchCount() {
                codeChecked = true;
                if (numberChecked) {
                    canClick();
                }
            }

            @Override
            public void onTextLessThanCount() {
                codeChecked = false;
                notClick();
            }
        });

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number = phoneNumber.getText().toString();
                code = codeEditText.getText().toString();
                if (Tools.isMobileNum(number)) {
                    if (!code.equals("")) {
                        showDialog("提交信息中");
                        int verifyType = 1;
                        if (type == TYPE_FORGET_PWD){
                            verifyType = 2;
                        }
                        AccountApi.verifyCode(number, code, verifyType,jsonHandlerVerify);
                    } else {
                        showToast("请输入验证码");
                    }
                } else {
                    showToast(R.string.not_moble_num);
                }
            }
        });
        notClick();
    }

    BaseJsonHandler codeHandler = new BaseJsonHandler(this) {

        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONObject data = response.optJSONObject("data");
            if (data.optInt("code") != 0) {
                showToast(data.optString("reasonPhrase"));
            } else {
                showToast("验证码发送成功");
            }
        }
    };

    BaseJsonHandler jsonHandlerVerify = new BaseJsonHandler(this) {

        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONObject data = response.optJSONObject("data");
            if (data.optInt("result") != 1) {
                showToast(data.optString("reasonPhrase"));
            } else {
                if (type == TYPE_REGISTER) {
                    Intent intent = new Intent(RegisterByMobileActivity.this, PasswordActivity.class);
                    intent.putExtra(PasswordActivity.EXTRAS_MOBILE, number);
                    intent.putExtra(PasswordActivity.EXTRAS_CODE, code);
                    intent.putExtra(PasswordActivity.EXTRAS_TYPE,PasswordActivity.TYPE_REGISTER);
                    startActivity(intent);
                    finish();
                } else if (type == TYPE_MODIFY) {
                    AccountApi.bindOtherAccount("mobile", number, "", jsonHandlerModify);
                } else if (type == TYPE_BIND) {
                    //bind
                    Intent intent = new Intent(RegisterByMobileActivity.this, PasswordActivity.class);
                    intent.putExtra(PasswordActivity.EXTRAS_MOBILE, number);
                    intent.putExtra(PasswordActivity.EXTRAS_CODE, code);
                    intent.putExtra(PasswordActivity.EXTRAS_TYPE,PasswordActivity.TYPE_BIND);
                    startActivity(intent);
                    finish();
                } else if (type == TYPE_FORGET_PWD) {
                    Intent intent = new Intent(RegisterByMobileActivity.this, PasswordActivity.class);
                    intent.putExtra(PasswordActivity.EXTRAS_MOBILE, number);
                    intent.putExtra(PasswordActivity.EXTRAS_CODE, code);
                    intent.putExtra(PasswordActivity.EXTRAS_TYPE,PasswordActivity.TYPE_FORGET_PWD);
                    startActivity(intent);
                    finish();
                }
            }
        }
    };

    BaseJsonHandler jsonHandlerModify = new BaseJsonHandler(this) {

        @Override
        public void onSuccessWithData(JSONObject response) {
            JSONObject data = response.optJSONObject("data");
            if (data.optInt("result") != 1) {
                showToast(data.optString("reasonPhrase"));
            } else {
                showToast("修改成功");
                finish();
            }
        }
    };

    private void canClick() {
        registerBtn.setBackgroundResource(R.drawable.btn_redbg);
        registerBtn.setClickable(true);
    }

    private void notClick() {
        registerBtn.setBackgroundResource(R.drawable.btn_unable);
        registerBtn.setClickable(false);
    }
}
